//
//  PrescriptionOrderTrackingNumberController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/13.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

class PrescriptionOrderTrackingNumberController: BaseViewController {
    
    var handleClosure: (() -> ())?
    
    private var viewModel = PrescriptionOrderDetailViewModel(mode: .detail)
    
    convenience init(id: String) {
        self.init()
        self.viewModel.prescriptionId = id
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "录入快递单"
    }
    
    override func makeUI() {
        super.makeUI()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide)
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        viewModel.changeClosure = { [weak self] in
            self?.handleClosure?()
            self?.navigationController?.popViewController(animated: true)
        }
    }

    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(PrescriptionOrderTrackingNumberCell.nibForCell(),
                           forCellReuseIdentifier: PrescriptionOrderTrackingNumberCell.reuseIdentifier())
        tableView.register(CommonButtonCell.nibForCell(),
                           forCellReuseIdentifier: CommonButtonCell.reuseIdentifier())
        
        return tableView
    }()

}

extension PrescriptionOrderTrackingNumberController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.row {
        case 0, 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: PrescriptionOrderTrackingNumberCell.reuseIdentifier(),
                                                     for: indexPath) as! PrescriptionOrderTrackingNumberCell
            cell.delegate = self
            cell.update(at: indexPath)
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: CommonButtonCell.reuseIdentifier(), for: indexPath) as! CommonButtonCell
            cell.delegate = self
            return cell
        default:
            return UITableViewCell()
        }
    }
}

extension PrescriptionOrderTrackingNumberController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.row {
        case 0, 1: return 50
        case 2:    return 90
        default:   return 0
        }
    }
}

extension PrescriptionOrderTrackingNumberController: CommonButtonCellDelegate, PrescriptionOrderTrackingNumberCellDelegate {
    func didTappedAction(_ cell: CommonButtonCell) {
        tableView.endEditing(true)
        
        guard !viewModel.backWaybillNo.isEmpty else {
            HUDTool.show(message: "快递单号未填写")
            return
        }
        
        guard !viewModel.backLogistics.isEmpty else {
            HUDTool.show(message: "快递公司未填写")
            return
        }
        
        viewModel.changePrescriptionOrderStatus(.sendingBack)
    }
    
    func didEndEditing(_ text: String, at indexPath: IndexPath) {
        switch indexPath.row {
        case 0: viewModel.backLogistics = text
        case 1: viewModel.backWaybillNo = text
        default:
            break
        }
    }
}


